#ifndef LOGTOVIEWERHANDLER_H
#define LOGTOVIEWERHANDLER_H

#include "../utilities_global.h"
#include "../zmqWrapper/publisher.h"
#include "LogBuffer.h"
#include "LogCollector.h"
#include "logcollectorconfig.h"
#include "logmodel.h"

class UTILITIESSHARED_EXPORT LogToViewerHandler : public QObject, public LogCollector
{
    Q_OBJECT
public:
    LogToViewerHandler();

    void init();

    ~LogToViewerHandler() override;

    Q_INVOKABLE void setLogLevel(QString category, int level);

    Q_INVOKABLE void updateLogLevel(QString category);

    Q_INVOKABLE QObject *getLogModel(QString category);

    Q_INVOKABLE QString getDefaultCategoryName() const
    {
        return defaultCategoryName;
    }

    // LogCollector interface
protected:
    virtual void onUnCategorizedLog(int logLevel, QString &msg) override;

    virtual void onCategorizedLog(QString &category, int logLevel, QString &msg) override;

    virtual void onMessage(QJsonObject &jsonObj) override;

    // QObject interface
protected:
    virtual void timerEvent(QTimerEvent *event) override;

private slots:
    void appendCategory(QString category);

signals:
    void appendCategorySignal(QString category);
    void detectLogBuffer();
    void newLogModelAttached(QString category);
    void logLevelPublished(QString category, int logLevel);

private:
    const QString warnLogName = "Warn";
    int detectLogBufferTimerID = -1;
    const int DetectLogBufferInterval = 180;
    QMap<int, QString> levelToColor;
    Publisher msgPublisher;
    QMap<QString, LogBuffer *> logBuffers;
    QMap<QString, LogModel *> logModels;
    LogCollectorConfig *config;
    ConfigFile *configFile;
};

#endif    // LOGTOVIEWERHANDLER_H
